﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Animation;

namespace SteelDetection
{
    public class CopyableTextBlock : TextBlock
    {
        private const int DoubleClickInterval = 250; // 双击间隔时间
        private long _lastClickTicks = 0;

        public CopyableTextBlock()
        {
            MouseLeftButtonDown += CopyableTextBlock_MouseLeftButtonDown;
        }

        private void CopyableTextBlock_MouseLeftButtonDown(object sender, MouseButtonEventArgs e)
        {
            long clickTicks = DateTime.Now.Ticks;
            long elapsedTicks = clickTicks - _lastClickTicks;
            _lastClickTicks = clickTicks;

            // 判断是否是双击事件
            if (elapsedTicks < DoubleClickInterval * 10000)
            {
                FlashText();
                // 获取文本内容
                string text = Text;

                // 将文本内容复制到剪贴板
                Clipboard.SetText(text);
            }
        }

        private void FlashText()
        {
            SolidColorBrush originalBrush = Foreground as SolidColorBrush;
            if (originalBrush == null)
                return;

            // 创建新的 SolidColorBrush 对象，以便于动画
            SolidColorBrush animatedBrush = new SolidColorBrush(originalBrush.Color);
            Foreground = animatedBrush;

            // 创建颜色动画
            ColorAnimation colorAnimation = new ColorAnimation
            {
                From = originalBrush.Color,
                To = Colors.LightSeaGreen, // 闪烁颜色
                Duration = TimeSpan.FromSeconds(0.5),
                AutoReverse = true,
                BeginTime = TimeSpan.Zero
            };

            // 将动画应用于文字的颜色和透明度
            animatedBrush.BeginAnimation(SolidColorBrush.ColorProperty, colorAnimation);

        }
    }
}
